package fy.uap.tree;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.dom4j.Element;

public class NodeBean {

	private String id;

	private String text;
	
	private boolean disabled;

	private List<NodeBean> items;
	
	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void addItems(Map<String, List<NodeBean>> nodeMap) {
		if (items != null) {
			for (int i = 0, size = items.size(); i < size; i++) {
				NodeBean node = items.get(i);
				node.setItems(nodeMap.get(node.getId()));
				node.addItems(nodeMap);
			}
		}
	}

	public List<NodeBean> getItems() {
		return items;
	}

	public void setItems(List<NodeBean> items) {
		this.items = items;
	}

	public void addItem(NodeBean item) {
		if (items == null) {
			items = new ArrayList<NodeBean>();
		} else
			for (int i = 0, length = items.size(); i < length; i++) {
				if (items.get(i).getId().equals(item.getId())) {
					return;
				}
			}
		items.add(item);
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public void toXMLElement(Element parent) {
		Element node = parent.addElement("item");
		node.addAttribute("id", id);

		node.addAttribute("text", text);
		
		if( disabled )
			node.addAttribute("disabled", String.valueOf(disabled));

		if (items != null) {
			for (int i = 0, size = items.size(); i < size; i++) {
				items.get(i).toXMLElement(node);
			}
		}
	}

	public void toXMLElement(Element parent, boolean disabled) {
		Element node = parent.addElement("item");
		node.addAttribute("id", id);

		node.addAttribute("text", text);

		node.addAttribute("disabled", String.valueOf(disabled));

		if (items != null) {
			for (int i = 0, size = items.size(); i < size; i++) {
				items.get(i).toXMLElement(node, disabled);
			}
		}
	}
}

